#include <iostream>
#include <string>
#include <cstring>
#include <cerrno>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include "comm.h"
bool makefifo()
{
    int n = mkfifo(FILENAME, 0666);
    if (n < 0)
    {
        std::cout << "mkfifo  fail!  errno:" << errno << ":" << strerror(errno) << std::endl;
        return false;
    }
    std::cout << "创建管道成功!" << std::endl;
    return true;
}
int main()
{
Start:
    int rfd = open(FILENAME, O_RDONLY);
    if (rfd < 0)
    {
        if (makefifo())
            goto Start;
        else
            return -1;
    }
    std::cout << "打开管道成功!" << std::endl;

    // 接下来只要读写即可
    char buffer[1024];
    while (true)
    {
        int ret = read(rfd, buffer, sizeof(buffer) - 1);
        if (ret > 0)
        {
            buffer[ret] = 0;
            std::cout << "client say#" << buffer << std::endl;
        }
        else if (ret == 0)
        {
            std::cout << "client  quit ,me too" << std::endl;
            break;
        }
        else
        {
            std::cout << "read  fail!  errno:" << errno << ":" << strerror(errno) << std::endl;
            break;
        }
    }
    close(rfd);
    return 0;
}